#!/usr/bin/python

# This is statement is required by the build system to query build info
if __name__ == '__build__':
	raise Exception


from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLE import *
import time
import array
import maintest
import chronos

vertices = array.array('f', [ -1,-1,1, -1,1,1, 1,1,1, 1,-1,1,\
  -1,-1,-1, -1,1,-1,  1,1,-1, 1,-1,-1 ] )

colors = array.array('f', [ 0, 0, 0,  1, 0, 0,  1, 1, 0,  0, 1, 0, \
  0, 0, 1,  1, 0, 1,  1, 1, 1,  0, 1, 1] )

cIndices = array.array('B', [0, 3, 2, 1,  2, 3, 7, 6,  0, 4, 7, 3, \
  1, 2, 6, 5,  4, 5, 6, 7,  0, 1, 5, 4 ] )	


angles = [0.0, 0.0, 0.0]
last_time = 0

def init():
    chronos.initAcc()
	

def RefreshAngle():
    global angles
    current_time = time.time()
    diff_time = current_time - last_time
    buff_angles = chronos.readAngles()
    if len(buff_angles) == 3:
       angles = buff_angles
       glutPostRedisplay()
    time.sleep(0.1)


# draw the polycone shape
def DrawStuff():
	global angles 
	rho = angles[0]
	phi = angles[1]
	theta = angles[2]
	#print "rho: " + str(rho)+ " phi: " + str(phi) + " theta: " + str(theta)
	# Phew. FINALLY, Draw the cube
	#glutSolidCube(10.0)
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
	glMatrixMode( GL_PROJECTION )
	glLoadIdentity( )
	glOrtho( -2, 2, -2, 2, -2, 2 )
	glMatrixMode( GL_MODELVIEW )
	glLoadIdentity( )
	glRotatef (rho, 1.0, 0.0, 0.0)
	glRotatef (-phi, 0.0, 0.0, 1.0)
	glEnableClientState( GL_COLOR_ARRAY )
	glEnableClientState( GL_VERTEX_ARRAY )
	glColorPointer( 3, GL_FLOAT, 0, colors.tostring( ) )
	glVertexPointer( 3, GL_FLOAT, 0, vertices.tostring( ) )
	glDrawElements( GL_QUADS, 24, GL_UNSIGNED_BYTE, cIndices.tostring( ) )
	#glDrawElements( GL_LINE_LOOP, 12, GL_UNSIGNED_BYTE, cIndices.tostring( ) )
	glDisableClientState( GL_COLOR_ARRAY )
	glDisableClientState( GL_VERTEX_ARRAY )
	glutWireCube(2.0)
	glutSwapBuffers( )


init()
maintest.main(DrawStuff, RefreshAngle)
